package com.aw.guiguzi.module.setting

import android.view.View
import android.widget.EditText
import android.widget.TextView
import com.aw.baselib.base.AwBaseBindingActivity
import com.aw.baselib.constants.Extras
import com.aw.baselib.util.AwDataUtil
import com.aw.baselib.util.AwSoftKeyboardUtil
import com.aw.guiguzi.R
import com.aw.guiguzi.constants.MyConstant
import com.aw.guiguzi.databinding.ActivitySettingCommonEditBinding
import com.aw.guiguzi.event.EventRefreshAnalysisLengthType
import com.aw.guiguzi.event.EventRefreshAnalysisScreenOutPreLengthType
import com.aw.guiguzi.event.EventRefreshCalDifferenceLengthType
import com.aw.guiguzi.event.EventRefreshCalResultType
import com.aw.guiguzi.event.EventRefreshLimitStatisticsCountLengthType
import com.aw.guiguzi.util.AppCacheUtil

class SettingCommonEditActivity : AwBaseBindingActivity<ActivitySettingCommonEditBinding?>() {
    private var type = 0
    override fun getViewBinding(): ActivitySettingCommonEditBinding {
        return ActivitySettingCommonEditBinding.inflate(layoutInflater)
    }

    override fun initView() {
        super.initView()
        type = intent.getIntExtra(Extras.params, -1)
        when (type) {
            MyConstant.TYPE_EDIT_ANALYSIS_SCREEN_OUT_PRE_NUMS_LENGTH -> setToolbarWithBackImgAndRightView(
                "自定义筛除长度",
                "提交"
            ) {
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) < 15 || AwDataUtil.getIntValue(
                        getText(binding?.etContent)
                    ) > 40
                ) {
                    showMsg("自定义分析数量需在15到40之间")
                    return@setToolbarWithBackImgAndRightView
                }
                AppCacheUtil.setAnalysisNumsScreenOutPreCustom(
                    AwDataUtil.getIntValue(
                        getText(
                            binding?.etContent
                        )
                    )
                )
                postEvent(EventRefreshAnalysisScreenOutPreLengthType(null))
                toFinishAndRefreshUI()
            }

            MyConstant.TYPE_EDIT_ANALYSIS_NUMS_LENGTH -> setToolbarWithBackImgAndRightView(
                "自定义分析长度配置",
                "提交"
            ) {
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) < 10 || AwDataUtil.getIntValue(
                        getText(binding?.etContent)
                    ) > 40
                ) {
                    showMsg("自定义分析数量需在10到40之间")
                    return@setToolbarWithBackImgAndRightView
                }
                AppCacheUtil.setAnalysisLengthCustom(AwDataUtil.getIntValue(getText(binding?.etContent)))
                postEvent(EventRefreshAnalysisLengthType(null))
                toFinishAndRefreshUI()
            }

            MyConstant.TYPE_EDIT_LIMIT_STATISTICS_COUNT_LENGTH -> setToolbarWithBackImgAndRightView(
                "自定义限制统计数量长度配置",
                "提交"
            ) {
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) < 8 || AwDataUtil.getIntValue(
                        getText(binding?.etContent)
                    ) > 15
                ) {
                    showMsg("自定义限制统计数量数字长度需在8到15之间")
                    return@setToolbarWithBackImgAndRightView
                }
                AppCacheUtil.setLimitStatisticsCountCustom(AwDataUtil.getIntValue(getText(binding?.etContent)))
                postEvent(EventRefreshLimitStatisticsCountLengthType(null))
                toFinishAndRefreshUI()
            }

            MyConstant.TYPE_EDIT_LENGTH_DIFFERENCE_ALL -> setToolbarWithBackImgAndRightView(
                "分析比值---所有数",
                "提交"
            ) {
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) < 5 || AwDataUtil.getIntValue(
                        getText(binding?.etContent)
                    ) > 20
                ) {
                    showMsg("自定义走势分析比值(所有数)需在5到20之间")
                    return@setToolbarWithBackImgAndRightView
                }
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) > 10) {
                    showDialogWithCancelDismiss("确定要设置为：" + getText(binding?.etContent) + "吗？值越大可推荐的数字几率越低。") { v: View? -> toSynCalLengthDifference() }
                } else {
                    toSynCalLengthDifference()
                }
            }

            MyConstant.TYPE_EDIT_LENGTH_DIFFERENCE_PRE15 -> setToolbarWithBackImgAndRightView(
                "分析比值---筛除前15位",
                "提交"
            ) {
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) < 5 || AwDataUtil.getIntValue(
                        getText(binding?.etContent)
                    ) > 20
                ) {
                    showMsg("自定义走势分析比值(筛除前15位)需在5到20之间")
                    return@setToolbarWithBackImgAndRightView
                }
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) > 10) {
                    showDialogWithCancelDismiss("确定要设置为：" + getText(binding?.etContent) + "吗？值越大可推荐的数字几率越低。") { v: View? -> toSynCalLengthDifference() }
                } else {
                    toSynCalLengthDifference()
                }
            }

            MyConstant.TYPE_EDIT_LENGTH_DIFFERENCE_LIMIT_ANALAYSIS -> setToolbarWithBackImgAndRightView(
                "分析比值---限制分析长度",
                "提交"
            ) {
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) < 3 || AwDataUtil.getIntValue(
                        getText(binding?.etContent)
                    ) > 15
                ) {
                    showMsg("自定义走势分析比值(限制分析长度)需在3到15之间")
                    return@setToolbarWithBackImgAndRightView
                }
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) > 8) {
                    showDialogWithCancelDismiss("确定要设置为：" + getText(binding?.etContent) + "吗？值越大可推荐的数字几率越低。") { v: View? -> toSynCalLengthDifference() }
                } else {
                    toSynCalLengthDifference()
                }
            }

            MyConstant.TYPE_EDIT_LENGTH_DIFFERENCE_LIMIT_STATISTICS -> setToolbarWithBackImgAndRightView(
                "分析比值---限制统计长度",
                "提交"
            ) {
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) < 3 || AwDataUtil.getIntValue(
                        getText(binding?.etContent)
                    ) > 10
                ) {
                    showMsg("自定义走势分析比值(限制统计长度)需在3到10之间")
                    return@setToolbarWithBackImgAndRightView
                }
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) > 6) {
                    showDialogWithCancelDismiss("确定要设置为：" + getText(binding?.etContent) + "吗？值越大可推荐的数字几率越低。") { v: View? -> toSynCalLengthDifference() }
                } else {
                    toSynCalLengthDifference()
                }
            }

            MyConstant.TYPE_EDIT_CAL_END_LENGTH -> setToolbarWithBackImgAndRightView(
                "分析比值---末尾连续长度",
                "提交"
            ) {
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) < 4 || AwDataUtil.getIntValue(
                        getText(binding?.etContent)
                    ) > 10
                ) {
                    showMsg("自定义走势分析比值(末尾连续长度)需在4到10之间")
                    return@setToolbarWithBackImgAndRightView
                }
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) > 8) {
                    showDialogWithCancelDismiss("确定要设置为：" + getText(binding?.etContent) + "吗？值越大可推荐的数字几率越低。") { v: View? -> toSynCalLengthDifference() }
                } else {
                    toSynCalLengthDifference()
                }
            }

            MyConstant.TYPE_EDIT_BETTER_TRENDS_PERCENT -> setToolbarWithBackImgAndRightView(
                "分析比值---择优比重",
                "提交"
            ) {
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) < 0 || AwDataUtil.getIntValue(
                        getText(binding?.etContent)
                    ) > 100
                ) {
                    showMsg("自定义走势分析比值(择优比重)需在0到100之间")
                    return@setToolbarWithBackImgAndRightView
                }
                if (AwDataUtil.getIntValue(getText(binding?.etContent)) < 50) {
                    showDialogWithCancelDismiss("确定要设置为：" + getText(binding?.etContent) + "%吗？值越小胜率越低。") { v: View? -> toSynCalLengthDifference() }
                } else {
                    toSynCalLengthDifference()
                }
            }
        }
    }

    override fun initData() {
        super.initData()
        when (type) {
            MyConstant.TYPE_EDIT_ANALYSIS_NUMS_LENGTH -> {
                setText(binding?.etContent, AppCacheUtil.getAnalysisLengthCustom().toString())
                setText(binding?.tvHint, getString(R.string.hint_setting_ui_analysis_count_length))
            }

            MyConstant.TYPE_EDIT_LIMIT_STATISTICS_COUNT_LENGTH -> {
                setText(binding?.etContent, AppCacheUtil.getLimitStatisticsCountCustom().toString())
                setText(binding?.tvHint, getString(R.string.hint_setting_ui_limit_count))
            }

            MyConstant.TYPE_EDIT_LENGTH_DIFFERENCE_ALL -> {
                setText(binding?.etContent, AppCacheUtil.getCalLengthDifferenceAll().toString())
                setText(binding?.tvHint, "*可设置范围5到20之间，不熟悉时需慎重更改")
            }

            MyConstant.TYPE_EDIT_LENGTH_DIFFERENCE_PRE15 -> {
                setText(binding?.etContent, AppCacheUtil.getCalLengthDifferencePre15().toString())
                setText(binding?.tvHint, "*可设置范围5到20之间，不熟悉时需慎重更改")
            }

            MyConstant.TYPE_EDIT_LENGTH_DIFFERENCE_LIMIT_ANALAYSIS -> {
                setText(binding?.etContent, AppCacheUtil.getCalLengthDifferenceLimitAnalaysis().toString())
                setText(binding?.tvHint, "*可设置范围3到15之间，不熟悉时需慎重更改")
            }

            MyConstant.TYPE_EDIT_LENGTH_DIFFERENCE_LIMIT_STATISTICS -> {
                setText(binding?.etContent, AppCacheUtil.getCalLengthDifferenceLimitStatistics().toString())
                setText(binding?.tvHint, "*可设置范围3到10之间，不熟悉时需慎重更改")
            }

            MyConstant.TYPE_EDIT_CAL_END_LENGTH -> {
                setText(binding?.etContent, AppCacheUtil.getCalEndLength().toString())
                setText(binding?.tvHint, "*可设置范围4到10之间，不熟悉时需慎重更改")
            }

            MyConstant.TYPE_EDIT_BETTER_TRENDS_PERCENT -> {
                setText(binding?.etContent, AppCacheUtil.getBetterTrendsPercent().toString())
                setText(
                    binding?.tvHint,
                    "*可设置范围0到100之间，不熟悉时需慎重更改\n最低数值建议：单押5个数（50%）、6个数（60%）...依次类推"
                )
            }
        }
        binding?.etContent!!.requestFocus()
    }

    override fun initListener() {
        super.initListener()
    }

    private fun toSynCalLengthDifference() {
        when (type) {
            MyConstant.TYPE_EDIT_LENGTH_DIFFERENCE_ALL -> AppCacheUtil.setCalLengthDifferenceAll(
                AwDataUtil.getIntValue(getText(binding?.etContent))
            )

            MyConstant.TYPE_EDIT_LENGTH_DIFFERENCE_PRE15 -> AppCacheUtil.setCalLengthDifferencePre15(
                AwDataUtil.getIntValue(getText(binding?.etContent))
            )

            MyConstant.TYPE_EDIT_LENGTH_DIFFERENCE_LIMIT_ANALAYSIS -> AppCacheUtil.setCalLengthDifferenceLimitAnalaysis(
                AwDataUtil.getIntValue(getText(binding?.etContent))
            )

            MyConstant.TYPE_EDIT_LENGTH_DIFFERENCE_LIMIT_STATISTICS -> AppCacheUtil.setCalLengthDifferenceLimitStatistics(
                AwDataUtil.getIntValue(getText(binding?.etContent))
            )

            MyConstant.TYPE_EDIT_CAL_END_LENGTH -> AppCacheUtil.setCalEndLength(
                AwDataUtil.getIntValue(
                    getText(binding?.etContent)
                )
            )

            MyConstant.TYPE_EDIT_BETTER_TRENDS_PERCENT -> AppCacheUtil.setBetterTrendsPercent(
                AwDataUtil.getIntValue(getText(binding?.etContent))
            )
        }
        postEvent(EventRefreshCalDifferenceLengthType(type))
        toFinishAndRefreshUI()
    }

    private fun toFinishAndRefreshUI() {
        postEvent(EventRefreshCalResultType(null))
        AwSoftKeyboardUtil.hideSoftKeyboard(mActivity)
        showMsg("设置成功")
        finish()
    }
}